package database

import "changeme/internal/models"

// DatabaseCapabilities 定义数据库能力查询接口
type DatabaseCapabilities interface {
	// 事务支持
	SupportsTransactions() bool

	// 结构支持
	SupportsSchemas() bool
	SupportsViews() bool
	SupportsStoredProcedures() bool
	SupportsFunctions() bool
	SupportsTriggers() bool

	// 查询特性
	SupportsCTEs() bool
	SupportsWindowFunctions() bool
	SupportsJSONType() bool
	SupportsArrayType() bool
	SupportsFullTextSearch() bool

	// 表特性
	SupportsPartitioning() bool
	SupportsForeignKeys() bool
	SupportsCheckConstraints() bool

	// 索引特性
	SupportsPartialIndexes() bool
	SupportsExpressionIndexes() bool

	// 获取所有能力作为映射
	GetAllCapabilities() map[string]bool
}

// BaseCapabilities 基础数据库能力实现
type BaseCapabilities struct{}

// SupportsTransactions 是否支持事务
func (bc *BaseCapabilities) SupportsTransactions() bool {
	return true
}

// SupportsSchemas 是否支持模式
func (bc *BaseCapabilities) SupportsSchemas() bool {
	return false
}

// SupportsViews 是否支持视图
func (bc *BaseCapabilities) SupportsViews() bool {
	return true
}

// SupportsStoredProcedures 是否支持存储过程
func (bc *BaseCapabilities) SupportsStoredProcedures() bool {
	return false
}

// SupportsFunctions 是否支持函数
func (bc *BaseCapabilities) SupportsFunctions() bool {
	return false
}

// SupportsTriggers 是否支持触发器
func (bc *BaseCapabilities) SupportsTriggers() bool {
	return false
}

// SupportsCTEs 是否支持公共表表达式
func (bc *BaseCapabilities) SupportsCTEs() bool {
	return false
}

// SupportsWindowFunctions 是否支持窗口函数
func (bc *BaseCapabilities) SupportsWindowFunctions() bool {
	return false
}

// SupportsJSONType 是否支持JSON类型
func (bc *BaseCapabilities) SupportsJSONType() bool {
	return false
}

// SupportsArrayType 是否支持数组类型
func (bc *BaseCapabilities) SupportsArrayType() bool {
	return false
}

// SupportsFullTextSearch 是否支持全文搜索
func (bc *BaseCapabilities) SupportsFullTextSearch() bool {
	return false
}

// SupportsPartitioning 是否支持分区
func (bc *BaseCapabilities) SupportsPartitioning() bool {
	return false
}

// SupportsForeignKeys 是否支持外键
func (bc *BaseCapabilities) SupportsForeignKeys() bool {
	return true
}

// SupportsCheckConstraints 是否支持检查约束
func (bc *BaseCapabilities) SupportsCheckConstraints() bool {
	return false
}

// SupportsPartialIndexes 是否支持部分索引
func (bc *BaseCapabilities) SupportsPartialIndexes() bool {
	return false
}

// SupportsExpressionIndexes 是否支持表达式索引
func (bc *BaseCapabilities) SupportsExpressionIndexes() bool {
	return false
}

// GetAllCapabilities 获取所有能力作为映射
func (bc *BaseCapabilities) GetAllCapabilities() map[string]bool {
	return map[string]bool{
		"supportsTransactions":      bc.SupportsTransactions(),
		"supportsSchemas":           bc.SupportsSchemas(),
		"supportsViews":             bc.SupportsViews(),
		"supportsStoredProcedures":  bc.SupportsStoredProcedures(),
		"supportsFunctions":         bc.SupportsFunctions(),
		"supportsTriggers":          bc.SupportsTriggers(),
		"supportsCTEs":              bc.SupportsCTEs(),
		"supportsWindowFunctions":   bc.SupportsWindowFunctions(),
		"supportsJSONType":          bc.SupportsJSONType(),
		"supportsArrayType":         bc.SupportsArrayType(),
		"supportsFullTextSearch":    bc.SupportsFullTextSearch(),
		"supportsPartitioning":      bc.SupportsPartitioning(),
		"supportsForeignKeys":       bc.SupportsForeignKeys(),
		"supportsCheckConstraints":  bc.SupportsCheckConstraints(),
		"supportsPartialIndexes":    bc.SupportsPartialIndexes(),
		"supportsExpressionIndexes": bc.SupportsExpressionIndexes(),
	}
}

// MySQLCapabilities MySQL能力实现
type MySQLCapabilities struct {
	BaseCapabilities
}

// SupportsViews MySQL支持视图
func (mc *MySQLCapabilities) SupportsViews() bool {
	return true
}

// SupportsStoredProcedures MySQL支持存储过程
func (mc *MySQLCapabilities) SupportsStoredProcedures() bool {
	return true
}

// SupportsFunctions MySQL支持函数
func (mc *MySQLCapabilities) SupportsFunctions() bool {
	return true
}

// SupportsTriggers MySQL支持触发器
func (mc *MySQLCapabilities) SupportsTriggers() bool {
	return true
}

// SupportsCTEs MySQL 8.0+ 支持CTEs
func (mc *MySQLCapabilities) SupportsCTEs() bool {
	return true
}

// SupportsWindowFunctions MySQL 8.0+ 支持窗口函数
func (mc *MySQLCapabilities) SupportsWindowFunctions() bool {
	return true
}

// SupportsFullTextSearch MySQL支持全文搜索
func (mc *MySQLCapabilities) SupportsFullTextSearch() bool {
	return true
}

// SupportsPartitioning MySQL支持分区
func (mc *MySQLCapabilities) SupportsPartitioning() bool {
	return true
}

// SupportsCheckConstraints MySQL 8.0.16+ 支持检查约束
func (mc *MySQLCapabilities) SupportsCheckConstraints() bool {
	return true
}

// SupportsJSONType 是否支持JSON类型
func (mc *MySQLCapabilities) SupportsJSONType() bool {
	return true
}

// SupportsArrayType 是否支持数组类型
func (mc *MySQLCapabilities) SupportsArrayType() bool {
	return true
}

// GetDefaultPort 获取默认端口
func (mc *MySQLCapabilities) GetDefaultPort() int {
	return 3306
}

// PostgreSQLCapabilities PostgreSQL能力实现
type PostgreSQLCapabilities struct {
	BaseCapabilities
}

// SupportsSchemas PostgreSQL支持模式
func (pc *PostgreSQLCapabilities) SupportsSchemas() bool {
	return true
}

// SupportsViews PostgreSQL支持视图
func (pc *PostgreSQLCapabilities) SupportsViews() bool {
	return true
}

// SupportsStoredProcedures PostgreSQL支持存储过程
func (pc *PostgreSQLCapabilities) SupportsStoredProcedures() bool {
	return true
}

// SupportsFunctions PostgreSQL支持函数
func (pc *PostgreSQLCapabilities) SupportsFunctions() bool {
	return true
}

// SupportsTriggers PostgreSQL支持触发器
func (pc *PostgreSQLCapabilities) SupportsTriggers() bool {
	return true
}

// SupportsCTEs PostgreSQL支持CTEs
func (pc *PostgreSQLCapabilities) SupportsCTEs() bool {
	return true
}

// SupportsWindowFunctions PostgreSQL支持窗口函数
func (pc *PostgreSQLCapabilities) SupportsWindowFunctions() bool {
	return true
}

// SupportsJSONType PostgreSQL支持JSON类型
func (pc *PostgreSQLCapabilities) SupportsJSONType() bool {
	return true
}

// SupportsArrayType PostgreSQL支持数组类型
func (pc *PostgreSQLCapabilities) SupportsArrayType() bool {
	return true
}

// SupportsFullTextSearch PostgreSQL支持全文搜索
func (pc *PostgreSQLCapabilities) SupportsFullTextSearch() bool {
	return true
}

// SupportsPartitioning PostgreSQL支持分区
func (pc *PostgreSQLCapabilities) SupportsPartitioning() bool {
	return true
}

// SupportsCheckConstraints PostgreSQL支持检查约束
func (pc *PostgreSQLCapabilities) SupportsCheckConstraints() bool {
	return true
}

// SupportsPartialIndexes PostgreSQL支持部分索引
func (pc *PostgreSQLCapabilities) SupportsPartialIndexes() bool {
	return true
}

// SupportsExpressionIndexes PostgreSQL支持表达式索引
func (pc *PostgreSQLCapabilities) SupportsExpressionIndexes() bool {
	return true
}

// GetMaxIdentifierLength 获取最大标识符长度
func (pc *PostgreSQLCapabilities) GetMaxIdentifierLength() int {
	return 63
}

// GetDefaultPort 获取默认端口
func (pc *PostgreSQLCapabilities) GetDefaultPort() int {
	return 5432
}

// SQLiteCapabilities SQLite能力实现
type SQLiteCapabilities struct {
	BaseCapabilities
}

// SupportsViews SQLite支持视图
func (sc *SQLiteCapabilities) SupportsViews() bool {
	return true
}

// SupportsTriggers SQLite支持触发器
func (sc *SQLiteCapabilities) SupportsTriggers() bool {
	return true
}

// SupportsCTEs SQLite支持CTEs
func (sc *SQLiteCapabilities) SupportsCTEs() bool {
	return true
}

// SupportsJSONType SQLite支持JSON类型（3.38.0+）
func (sc *SQLiteCapabilities) SupportsJSONType() bool {
	return true
}

// SupportsFullTextSearch SQLite支持全文搜索
func (sc *SQLiteCapabilities) SupportsFullTextSearch() bool {
	return true
}

// SupportsPartialIndexes SQLite支持部分索引
func (sc *SQLiteCapabilities) SupportsPartialIndexes() bool {
	return true
}

// SupportsExpressionIndexes SQLite支持表达式索引
func (sc *SQLiteCapabilities) SupportsExpressionIndexes() bool {
	return true
}

// GetMaxIdentifierLength 获取最大标识符长度
func (sc *SQLiteCapabilities) GetMaxIdentifierLength() int {
	return 255
}

// GetDefaultPort 获取默认端口
func (sc *SQLiteCapabilities) GetDefaultPort() int {
	return 0 // 文件数据库
}

// SQLServerCapabilities SQL Server能力实现
type SQLServerCapabilities struct {
	BaseCapabilities
}

// SupportsSchemas SQL Server支持模式
func (sc *SQLServerCapabilities) SupportsSchemas() bool {
	return true
}

// SupportsViews SQL Server支持视图
func (sc *SQLServerCapabilities) SupportsViews() bool {
	return true
}

// SupportsStoredProcedures SQL Server支持存储过程
func (sc *SQLServerCapabilities) SupportsStoredProcedures() bool {
	return true
}

// SupportsFunctions SQL Server支持函数
func (sc *SQLServerCapabilities) SupportsFunctions() bool {
	return true
}

// SupportsTriggers SQL Server支持触发器
func (sc *SQLServerCapabilities) SupportsTriggers() bool {
	return true
}

// SupportsCTEs SQL Server支持CTEs
func (sc *SQLServerCapabilities) SupportsCTEs() bool {
	return true
}

// SupportsWindowFunctions SQL Server支持窗口函数
func (sc *SQLServerCapabilities) SupportsWindowFunctions() bool {
	return true
}

// SupportsJSONType SQL Server支持JSON类型（2016+）
func (sc *SQLServerCapabilities) SupportsJSONType() bool {
	return true
}

// SupportsFullTextSearch SQL Server支持全文搜索
func (sc *SQLServerCapabilities) SupportsFullTextSearch() bool {
	return true
}

// SupportsPartitioning SQL Server支持分区
func (sc *SQLServerCapabilities) SupportsPartitioning() bool {
	return true
}

// SupportsCheckConstraints SQL Server支持检查约束
func (sc *SQLServerCapabilities) SupportsCheckConstraints() bool {
	return true
}

// DaMengCapabilities 达梦数据库能力实现
type DaMengCapabilities struct {
	BaseCapabilities
}

// SupportsSchemas 达梦支持模式
func (dc *DaMengCapabilities) SupportsSchemas() bool {
	return true
}

// SupportsViews 达梦支持视图
func (dc *DaMengCapabilities) SupportsViews() bool {
	return true
}

// SupportsStoredProcedures 达梦支持存储过程
func (dc *DaMengCapabilities) SupportsStoredProcedures() bool {
	return true
}

// SupportsFunctions 达梦支持函数
func (dc *DaMengCapabilities) SupportsFunctions() bool {
	return true
}

// SupportsTriggers 达梦支持触发器
func (dc *DaMengCapabilities) SupportsTriggers() bool {
	return true
}

// SupportsCTEs 达梦支持CTEs
func (dc *DaMengCapabilities) SupportsCTEs() bool {
	return true
}

// SupportsWindowFunctions 达梦支持窗口函数
func (dc *DaMengCapabilities) SupportsWindowFunctions() bool {
	return true
}

// SupportsFullTextSearch 达梦支持全文搜索
func (dc *DaMengCapabilities) SupportsFullTextSearch() bool {
	return true
}

// SupportsPartitioning 达梦支持分区
func (dc *DaMengCapabilities) SupportsPartitioning() bool {
	return true
}

// SupportsCheckConstraints 达梦支持检查约束
func (dc *DaMengCapabilities) SupportsCheckConstraints() bool {
	return true
}

// GetMaxIdentifierLength 获取最大标识符长度
func (dc *DaMengCapabilities) GetMaxIdentifierLength() int {
	return 128
}

// GetDefaultPort 获取默认端口
func (dc *DaMengCapabilities) GetDefaultPort() int {
	return 5236
}

// GetCapabilities 根据数据库类型获取能力实现
func GetCapabilities(dbType models.DatabaseType) DatabaseCapabilities {
	switch dbType {
	case models.MySQL:
		return &MySQLCapabilities{}
	case models.PostgreSQL:
		return &PostgreSQLCapabilities{}
	case models.SQLite:
		return &SQLiteCapabilities{}
	case models.SQLServer:
		return &SQLServerCapabilities{}
	case models.DaMeng:
		return &DaMengCapabilities{}
	default:
		return &BaseCapabilities{}
	}
}
